package random;

/**
 * 转置矩阵
 *
 * 你一个二维整数数组 matrix， 返回 matrix 的 转置矩阵 。
 * 矩阵的 转置 是指将矩阵的主对角线翻转，交换矩阵的行索引与列索引。
 */

public class L867 {

    public static void main(String[] args) {

        int[][] matrix = {{1,2,3},{4,5,6},{7,8,9}};
//        int[][] matrix = {{1,2,3},{4,5,6}};

        int[][] transpose = transpose(matrix);
        for (int i = 0; i < transpose.length; i++) {
            for (int j = 0; j < transpose[i].length; j++) {
                System.out.print(transpose[i][j]);
            }
        }
    }

    public static int[][] transpose(int[][] matrix) {

        int row = matrix[0].length;
        int column = matrix.length;
        int[][] reverse = new int[row][column];

        for (int i = 0; i < matrix.length; i++) {
            for (int j = 0; j < matrix[i].length; j++) {
                reverse[j][i] = matrix[i][j];
            }
        }
        return reverse;
    }
}
